package com.fast.fastenjoy.base.manager;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.commons.lang.StringUtils;

import com.fast.fastenjoy.base.utils.ThreadTaskUtils;
import com.fast.fastenjoy.base.utils.ThreadTaskUtils.TaskBean;

/**
 * 缓存处理父类
 * @author chenliaohua
 *
 * @param <T>
 */
public abstract class CacheEntityManager<T extends Serializable> extends EntityManager<T>{

	private static final Map<String, Object> cache = new HashMap<String, Object>();
	
	/**
	 * 缓存数据
	 * @param key
	 * @param obj
	 */
	public void cache(String key, Object obj){
		
		if(StringUtils.isEmpty(key)){
			return;
		}
		
		cache.put(key, obj);
	}
	
	/**
	 * 获取缓存数据
	 * @param key
	 * @return
	 */
	public Object getCache(String key){
		
		if(StringUtils.isEmpty(key)){
			return null;
		}
		
		Object object = cache.get(key);
		
		if(object == null){
			refCache();
		}
		
		return cache.get(key);
	}
	
	/**
	 * 异步刷新缓存数据
	 */
	public void asyncRefCache(){
		ThreadTaskUtils.getInstance(new TaskBean() {
			public void startTask(Object... objects) {
				refCache();
			}
		}).startTask();
	}
	
	@PostConstruct
	public abstract void refCache();
	
}
